
import React from 'react';

export interface ILazyProps {
    factory: () => Promise<any>;
}

export default function Lazy<IProps = {}>(props: ILazyProps & IProps) {

    const { factory } = props;

    const [Ct, setCt] = React.useState<any>();

    const appProps = React.useMemo<IProps>(() => ({ ...props, factory: undefined }), [props])

    const Default = React.useMemo(() => Ct?.default, [Ct]);

    React.useEffect(() => {
        factory().then(setCt)
    }, [factory]);

    return Default ? <Default {...appProps} /> : <></>;
}

